//
//  MyCouponViewController.m
//  LoveBeeProject
//
//  Created by 沈腾飞 on 2017/1/5.
//  Copyright © 2017年 斯芬克斯. All rights reserved.
//

#import "MyCouponViewController.h"
#import "MyCouponModel.h"
#import <Masonry.h>
#import "MyCouponTableViewCell.h"
#import <MBProgressHUD.h>
static NSString * couponCellID = @"couponCellID";
static NSString * myCouponCellID = @"myCouponCellID";
@interface MyCouponViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,weak) UITableView *couponView;
@property (nonatomic, strong) UIVisualEffectView *effectView;
@end

@implementation MyCouponViewController{
    NSArray *_newsList;
    NSMutableArray *_effctionArr;
    NSMutableArray *_noEffectArr;
}

- (void)loadData{
    NSString *url = @"http://iosapi.itcast.cn/loveBeen/MyCoupon.json.php";
    NSDictionary *parameters = [NSDictionary dictionaryWithObject:@"9" forKey:@"call"];
    [MyCouponModel loadNewsDataWithUrlStr:url parameters:parameters success:^(NSArray *newsList) {
        _newsList = newsList;
        
        [self addModelArr];
        [self.couponView reloadData];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self.effectView removeFromSuperview];
        
        
    } failed:^(NSError *error) {
        NSLog(@"%@",error);
    }];


}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpUI];
    [self loadData];
    UIBlurEffect *beffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc]initWithEffect:beffect];
    effectView.frame = CGRectMake(0, 44, self.view.bounds.size.width, self.view.bounds.size.height - 44);
    [self.view addSubview:effectView];
    self.effectView = effectView;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
}
- (void)setUpUI{
    UITableView *couponView = [[UITableView alloc] init];
    self.couponView = couponView;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"使用规则" style:UIBarButtonItemStyleDone target:self action:nil];
    [self.navigationItem.rightBarButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:12], NSFontAttributeName, nil] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor grayColor];
    //设置数据源和代理
    couponView.delegate = self;
    couponView.dataSource = self;
    //自适应行高
    couponView.rowHeight = UITableViewAutomaticDimension;
    
    //预估行高
    couponView.estimatedRowHeight = 120;
    //    couponView.rowHeight = 120;
    
    [couponView registerNib:[UINib nibWithNibName:@"MyCouponTableViewCell" bundle:nil] forCellReuseIdentifier:couponCellID];
    [couponView registerNib:[UINib nibWithNibName:@"MyCouponCell" bundle:nil] forCellReuseIdentifier:myCouponCellID];
    [self.view addSubview:couponView];
    couponView.separatorStyle = UITableViewCellSeparatorStyleNone;
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 40)];
    
    self.couponView.tableHeaderView = headerView;
    
    UITextField *textF = [[UITextField alloc] init];
    textF.keyboardType = UIKeyboardTypeNumberPad;
    textF.backgroundColor = [UIColor whiteColor];
    textF.borderStyle = UITextBorderStyleRoundedRect;
    textF.placeholder = @"请输入优惠券号码";
    [headerView addSubview:textF];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.layer.cornerRadius = 2.5;
    btn.backgroundColor = [UIColor cz_colorWithRed:254 green:211 blue:51];
    [btn setTitle:@"绑定" forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:14.f];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [headerView addSubview:btn];
    UIView *topView = [[UIView alloc] init];
    UIView *bottomView = [[UIView alloc] init];
    topView.backgroundColor = [UIColor cz_colorWithRed:222 green:222 blue:222];
    bottomView.backgroundColor = [UIColor cz_colorWithRed:222 green:222 blue:222];
    [headerView addSubview:topView];
    [headerView addSubview:bottomView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(headerView.mas_top);
        make.height.offset(1);
        
    }];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.bottom.equalTo(headerView.mas_bottom);
        make.height.offset(1);
        
    }];
    
    [textF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(20);
        make.centerY.offset(0);
        make.right.equalTo(btn.mas_left).offset(-10);
        make.height.offset(30);
        
    }];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(textF.mas_top);
        make.bottom.equalTo(textF.mas_bottom);
        make.right.offset(-10);
        make.width.offset(50);
    }];
    //布局
    [couponView mas_makeConstraints:^(MASConstraintMaker *make) {
        [make edges];
    }];
}
- (void)addModelArr{
    _effctionArr = [NSMutableArray array];
    _noEffectArr = [NSMutableArray array];
    for (int i = 0; i < _newsList.count; i++) {
        MyCouponModel *model = _newsList[i];
        if ([model.is_userd isEqualToString:@"1"] ) {
            [_effctionArr addObject:model];
        }else{
            [_noEffectArr addObject:model];
        
        }
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return _effctionArr.count;
    }else{
        return _noEffectArr.count;
    }
    return _newsList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *identifier;

    if(indexPath.section == 0){
        identifier =myCouponCellID;
    }else{
        identifier = couponCellID;
    }

    
    MyCouponTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    MyCouponModel *model = [MyCouponModel new];
    if(indexPath.section == 0){
        model = _effctionArr[indexPath.row];
    }else {
        model = _noEffectArr[indexPath.row];
    }
    
    cell.model = model;
    return cell;
}


@end
